﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using GoogleReaderAccess;

namespace GoogleReaderClient
{
    delegate void SetTextCallback(TextBox textBox, string text);
    delegate void SetTextBlockCallback(TextBlock textBlock, string text);

    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
            gr = new GoogleReader(textBoxUser.Text, textBoxPassword.Password);
            gr.LoginDone += new LoginExecuted(HandleLoginEvent);
            gr.SubscriptionFailed += new SubscriptionFetchFailed(HandleSubscriptionFailedEvent);
            gr.SubscriptionReady += new SubscriptionReady(HandleSubscriptionReadyEvent);      
        }

        /// <summary>
        /// Event raised when pressing button
        /// </summary>
        /// <param name="sender">The sender</param>
        /// <param name="e">The event args</param>
        private void buttonLogin_Click(object sender, RoutedEventArgs e)
        {
            if (textBoxUser.Text.Length > 0 && textBoxPassword.Password.Length > 0)
            {
                gr.login();
            }
        }


        /// <summary>
        /// Handles loging events from the GoogleReaderAcces library
        /// </summary>
        /// <param name="sender">Sender of the event</param>
        /// <param name="e">The login information</param>
        private void HandleLoginEvent(object sender, LoginEventArgs e)
        {
            string str = "";
            if(e.Ok)
                str += "Login executed OK";
            else
                str += "Login failed: "+e.Message;
            SetTextCallback cb = new SetTextCallback(ChangeTextBoxText);
            this.Dispatcher.BeginInvoke(cb, new object[] { textBoxLoginResult, str });
        }

        private void HandleSubscriptionFailedEvent(object sender, SubscriptionsEventArgs e)
        {
            SetTextCallback cb = new SetTextCallback(ChangeTextBoxText);
            this.Dispatcher.BeginInvoke(cb, new object[] { textBoxLoginResult, e.Message });
        }

        private void HandleSubscriptionReadyEvent(object sender, SubscriptionListData data)
        {
            SetTextBlockCallback cb = new SetTextBlockCallback(ChangeTextBlockText);
            this.Dispatcher.BeginInvoke(cb, new object[] { textBlockXml, data.Content });
        }


        /// <summary>
        /// Delegate used for changing UI textbox text from the correct thread.
        /// </summary>
        /// <param name="textBox">Text box that will have its Text property changed</param>
        /// <param name="text">The text to insert.</param>
        private void ChangeTextBoxText(TextBox textBox, string text)
        {
            textBox.Text = text;
        }
        
        private void ChangeTextBlockText(TextBlock textBlock, string text)
        {
            textBlock.Text += text;
        }

        GoogleReader gr;

        private void buttonSubsList_Click(object sender, RoutedEventArgs e)
        {
            gr.getSubscriptionList();
        }
    }
}